// subpkg_worker/medicine_record/medicine_record.js
const request = require('../../utils/request.js')
const url = require('../../utils/link.js')
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    elderId:'',
    medicationId:'',
    chooseBgcolor:'red',
    chooseBgcolor2:'white',
    activeName: '1',
    display1:'none',
    display2:'none',
    display_show1:'block',
    display_show2:'none',
    medicineList:[],
    listId:'',
    recordList:[],
    //存药单记录id
    ids:[],

    id:''
  },

  changeShow1(){
    this.setData({
      display_show2:'none',
      display_show1:'block',
      chooseBgcolor:'red',
      chooseBgcolor2:'white'

    })
  },
  changeShow2(){
    this.setData({
      display_show1:'none',
      display_show2:'block',
      chooseBgcolor:'white',
      chooseBgcolor2:'red'
    })
  },

  onChange(event) {
    this.setData({
      activeName: event.detail,
    });
    var index = event.currentTarget.dataset.index
    this.setData({
      listId:this.data.ids[index]
    })
  },
  addMedicine(){
    if(this.data.display1==='none'){
      this.setData({
        display1:'block'
      })
    }else{
      this.setData({
        display2:'block'
      })
    }
     
  },

//更新用药
  record(e){
      var index = e.currentTarget.dataset.index
      console.log(index)
      var list_num = this.data.medicineList[index].id
      console.log(list_num)
      var listId = this.data.listId
      for(var i in this.data.recordList){
        if(this.data.recordList[i].list_num === list_num){
          var time = this.data.recordList[i].time
          var time1 = parseInt(time.slice(0,2))
          var date = new Date()
          var hour = date.getHours()
          console.log(hour)
          console.log(time1)
          if(hour>=time1){
            this.setData({
              id:this.data.recordList[i].id
            })
          }
        }
      }
      if(this.data.id===''){
        Toast('未到服药时间!')
        return
      }
      wx.request({
        url: `https://zhixiao.site:8080/client/medication_record/update?id=${this.data.id}&status=1`,
        method:'PUT',
        // header:{
        //   'Content-Type':'application/json'
        // },
        // data:
        // {
        //   id:this.data.listId,
        //   status:0
        // },
        success:(res)=>{
          Toast('服用成功');
          console.log(res)
        }
      })
    
   
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //this.query()
    if(options.medicationId!=undefined){
      this.setData({
        medicationId:options.medicationId
      })
    }
    this.queryMedicineList(options.id)
    
  },
  //根据老人id获取当天的用药记录
  queryToday(listNum){
    const that = this
    wx.request({
      url: 'https://zhixiao.site:8080/client/medication_record/selectToday',
      data:{
        elderlyId:1
      },
      success:(res)=>{
        console.log(11)
        console.log(res.data.data)
      },
      complete:(res)=>{
        console.log(res)
        const recordList = that.data.recordList
        var recordList1 = res.data.data
        const ids = []
        console.log(99)
        for(var i in recordList1){
          if(recordList1[i].listNum===listNum){
            console.log(recordList1)
            recordList.push({id:recordList1[i].id,list_num:recordList1[i].medicationList.id,status:recordList1[i].status,time:recordList1[i].time,listId:recordList1[i].medicationListId})
            if(ids.indexOf(recordList1[i].medicationListId)===-1){
              ids.push(recordList1[i].medicationListId)
            }
          }
        }
        this.setData({
          ids:ids
        })
        const medicineList = this.data.medicineList
        for(var i in medicineList){
          // if(medicineList[i].medication.id === this.data.medicationId){
          //   console.log(i)
          // }
          if(medicineList[i].medication.id === this.data.medicationId){
            console.log(i)
            this.setData({
              activeName: parseInt(i) + 1
            })
          }
        }

      }
    })
  },
  //获取老人正在服用的药单项
  queryMedicineList(id){
    const that = this
    wx.request({
      url: 'https://zhixiao.site:8080/client/medication_list/query',
      data:{
        elderlyId:'1'
      },
      success:(res)=>{
        var medicineList = res.data.data
        for(var i in medicineList){
          if(i === id){
            console.log(id)
            console.log(medicineList[i])
            console.log(777)
            that.setData({
              //设置listId
              medicineList:medicineList[i]
            })
            for(var j in medicineList[i]){
              var listId = medicineList[i][j].id
              console.log(listId)
              //根据listId生成用药记录
              this.add(listId)
            }
          }
        }
      },
      complete:(res)=>{
        that.queryToday(id)
      }
    })
  },


  
  //根据药物id生成用药记录
  add(id){
    console.log(33)
    wx.request({
      url: 'https://zhixiao.site:8080/client/medication_record/add',
      method:'POST',
      header:{
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data:{
        listId:id
      },
      success:(res)=>{
        console.log(res)
        console.log(222)
      }
    })
  },
  query(){
    const that = this
    wx.request({
      url: 'https://zhixiao.site:8080/client/medicineName/query',
      success:(res)=>{
        var option= []
        for(var i = 0;i<res.data.data.length;i++){
            option.push({text:res.data.data[i],value:option.length})
        }
        this.setData({
          option0:option
        })
      }
    })
  },

  
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})